package com.sit.utils;

import jakarta.servlet.http.HttpSession;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

public class CaptchaImgUtil {
    static String code = "";

    // 将验证码存储在会话中，并对其进行加密处理
    public static void storeCaptchaInSession(HttpSession session, int length) throws Exception {
        String captcha = CaptchaUtil.generateCaptcha(length);
        code = captcha;
        String SHA_encryptedCaptcha = CaptchaUtil.encryptCaptcha(captcha);
        String AES_encryptedCaptcha = CaptchaUtil.encrypt(captcha);
        session.setAttribute("ShaCaptcha", SHA_encryptedCaptcha);
        session.setAttribute("AesCaptcha", AES_encryptedCaptcha);
    }

    // 生成验证码图片
    public static byte[] generateCaptchaImage(int width, int height, int length) throws IOException {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        // 设置背景色
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);
        // 绘制边框
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, width - 1, height - 1);
        // 生成验证码
        String captcha = code;
        // 绘制验证码
        drawCaptcha(g, captcha, width, height);
        // 添加干扰元素
        addNoise(g, width, height);
        // 将图像转换为字节数组
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", baos);
        return baos.toByteArray();
    }

    // 绘制验证码，随机化字体类型、大小和样式
    private static void drawCaptcha(Graphics g, String captcha, int width, int height) {
        Random random = new Random();
        for (int i = 0; i < captcha.length(); i++) {
            Font font = new Font(getRandomFontFamily(), Font.BOLD + random.nextInt(3), 20 + random.nextInt(20));
            g.setFont(font);
            g.setColor(getRandomColor());
            g.drawString(captcha.charAt(i) + "", (i * 20) + 10, (height / 2) + random.nextInt(10));
        }
    }

    // 添加干扰元素：干扰线和干扰点
    private static void addNoise(Graphics g, int width, int height) {
        Random random = new Random();
        // 绘制干扰线
        for (int i = 0; i < 5; i++) {
            g.setColor(getRandomColor());
            int x1 = random.nextInt(width);
            int y1 = random.nextInt(height);
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            g.drawLine(x1, y1, x2, y2);
        }
        // 绘制干扰点
        for (int i = 0; i < 50; i++) {
            g.setColor(getRandomColor());
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            g.drawOval(x, y, 2, 2);
        }
    }

    // 获取随机颜色
    private static Color getRandomColor() {
        Random random = new Random();
        return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
    }

    // 获取随机字体
    private static String getRandomFontFamily() {
        String[] fontFamilies = {"Arial", "Verdana", "Times New Roman", "Courier New", "Georgia"};
        Random random = new Random();
        return fontFamilies[random.nextInt(fontFamilies.length)];
    }
}
